<?php

declare(strict_types=1);

namespace app\listener;

use app\model\User;
use app\tool\Convert;
use app\tool\Msg;
use Swoole\Http\Request;
use Swoole\WebSocket\Server;
use think\facade\Cache;
use think\facade\Log;
use think\swoole\Websocket;

class SwWsMessage
{
    /**
     * 事件监听处理
     *
     * @return mixed
     */
    public function handle($event, Websocket $ws, \app\Request $request)
    {
        $fd = $ws->getSender();
        $server = $request->server();
        $ip = $server['REMOTE_ADDR'];

        var_dump($event);

        //二进制数据流
        if ($event->opcode == 2) {
            $param = Cache::get('param_' . $fd);
            $param = json_decode($param, true);

            Convert::convert($ws, $fd, $param, $event);
        } elseif ($event->opcode == 3) {
            //视频流处理和转发
            $exec = "ffmpeg -i ";

        } else {
            //缓存文件名
            Cache::set('param_' . $fd, $event->data);

            $dataArr = json_decode($event->data, true);

            //根据类型处理相关内容
            if (isset($dataArr['type'])) {
                Msg::doType($dataArr, $ws, $fd, $ip);
            }
        }
    }
}
